package com.zky.controller;

import com.zky.entity.Hotel;
import com.zky.service.HotelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/hotels")
public class HotelController {
    
    @Autowired
    private HotelService hotelService;
    
    @GetMapping
    public ResponseEntity<List<Hotel>> getAllHotels(
            @RequestParam(required = false) String keyword) {
        try {
            List<Hotel> hotels = hotelService.searchHotels(keyword);
            return ResponseEntity.ok(hotels);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }
    
    @GetMapping("/{id}")
    public ResponseEntity<Hotel> getHotelById(@PathVariable Integer id) {
        return ResponseEntity.ok(hotelService.getHotelById(id));
    }
    
    @PostMapping
    public ResponseEntity<?> createHotel(@RequestBody Hotel hotel) {
        try {
            hotelService.createHotel(hotel);
            return ResponseEntity.ok().body(Map.of(
                "message", "创建成功",
                "data", hotel
            ));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(Map.of(
                "message", "创建失败: " + e.getMessage()
            ));
        }
    }
    
    @PutMapping("/{id}")
    public ResponseEntity<?> updateHotel(@PathVariable Integer id, @RequestBody Hotel hotel) {
        try {
            hotel.setId(id);
            hotelService.updateHotel(hotel);
            return ResponseEntity.ok().body(Map.of(
                "message", "更新成功",
                "data", hotel
            ));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(Map.of(
                "message", "更新失败: " + e.getMessage()
            ));
        }
    }
    
    @DeleteMapping("/{id}")
    public ResponseEntity<?> deleteHotel(@PathVariable Integer id) {
        try {
            hotelService.deleteHotel(id);
            return ResponseEntity.ok().body(Map.of(
                "message", "删除成功"
            ));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(Map.of(
                "message", "删除失败: " + e.getMessage()
            ));
        }
    }
} 